package nccloud.web.ic.custrecloss.action.transfer;

import java.math.BigDecimal;
import nc.vo.ic.m4y.entity.TransOutBodyVO;
import nc.vo.ic.m4y.entity.TransOutVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.scmpub.util.BillBodySortUtils;
import nccloud.base.exception.ExceptionUtils;
import nccloud.dto.scmpub.page.entity.SCMQueryTreeFormatVO;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.pattern.billgrid.BillGrid;
import nccloud.framework.web.ui.pattern.billgrid.BillGridOperator;
import nccloud.pubitf.ic.custrecloss.service.IWastAgeFetchBillService;
import nccloud.web.ic.outbound.allocationout.action.AllocationOutBillGridPrecisionHandler;
import nccloud.web.ic.pub.precision.ICBaseBillGridPrecisionHandler;








public class WastAgeQryTransOutAction
  implements ICommonAction
{
  public Object doAction(IRequest request) {
    IJson json = JsonFactory.create();
    SCMQueryTreeFormatVO queryinfo = (SCMQueryTreeFormatVO)json.fromJson(request.read(), SCMQueryTreeFormatVO.class);

    
    IWastAgeFetchBillService service = (IWastAgeFetchBillService)ServiceLocator.find(IWastAgeFetchBillService.class);
    TransOutVO[] vos = null;
    try {
      vos = service.queryTransOutBill(queryinfo.getQueryInfo());
    } catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    } 
    if (vos == null || vos.length<1) {
      return null;
    }
    BillBodySortUtils.sort(vos);
    
    BillGrid[] cards = (new BillGridOperator(queryinfo.getTempletid(), queryinfo.getPageCode())).toBillGrids(vos);
    processResult(cards, vos);
    return cards;
  }








  
  public void processResult(BillGrid[] cards, TransOutVO[] vos) {
    for (int i = 0; i < cards.length; i++) {
      BillGrid billgrid = cards[i];
      
      ICBaseBillGridPrecisionHandler handler = new AllocationOutBillGridPrecisionHandler(billgrid);
      
      handler.process();

      
      TransOutVO vo = vos[i];
      for (int j = 0; j < vo.getBodys().length; j++) {
        TransOutBodyVO body = vo.getBody(j);
        
        UFDouble ntranoutnum = body.getNtranoutnum();
        Double ncaninnum = Double.valueOf((ntranoutnum != null) ? ntranoutnum.toDouble().doubleValue() : 0.0D);
        Cell cell = new Cell();
        cell.setValue(ncaninnum);

        
        int scale = billgrid.getBody().getModel().getRows()[j].getCell("ntranoutnum").getScale();
        cell.setScale(scale);
        
        BigDecimal display = (new BigDecimal(ncaninnum.doubleValue())).setScale(scale, 4);
        
        cell.setDisplay(display.toString());
        billgrid.getBody().getModel().getRows()[j].addCell("ncaninnum", cell);
      } 
    } 
  }
}